//
//  FCCConversationCell.swift
//  chatUIDemo
//
//  Created by admin on 2025/7/10.
//

import UIKit
import SnapKit

class FCCConversationCell: UITableViewCell {
    
    // MARK: - UI组件
    private let qfcAvatarImageView = UIImageView()
    private let qfcUserNameLabel = UILabel()
    private let qfcLastMessageLabel = UILabel()
    private let qfcTimestampLabel = UILabel()
    private let qfcUnreadBadge = UIView()
    private let qfcUnreadCountLabel = UILabel()
    
    // MARK: - 初始化
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        qfcSetupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI设置
    private func qfcSetupUI() {
        // 头像
        qfcAvatarImageView.layer.cornerRadius = 25
        qfcAvatarImageView.clipsToBounds = true
        qfcAvatarImageView.contentMode = .scaleAspectFill
        qfcAvatarImageView.backgroundColor = .lightGray
        
        // 用户名
        qfcUserNameLabel.font = UIFont.boldSystemFont(ofSize: 16)
        
        // 最后一条消息
        qfcLastMessageLabel.font = UIFont.systemFont(ofSize: 14)
        qfcLastMessageLabel.textColor = .darkGray
        qfcLastMessageLabel.numberOfLines = 1
        
        // 时间戳
        qfcTimestampLabel.font = UIFont.systemFont(ofSize: 12)
        qfcTimestampLabel.textColor = .gray
        qfcTimestampLabel.textAlignment = .right
        
        // 未读消息徽章
        qfcUnreadBadge.backgroundColor = .systemRed
        qfcUnreadBadge.layer.cornerRadius = 10
        qfcUnreadBadge.clipsToBounds = true
        qfcUnreadBadge.isHidden = true
        
        // 未读消息数量
        qfcUnreadCountLabel.font = UIFont.boldSystemFont(ofSize: 12)
        qfcUnreadCountLabel.textColor = .white
        qfcUnreadCountLabel.textAlignment = .center
        
        // 添加子视图
        contentView.addSubview(qfcAvatarImageView)
        contentView.addSubview(qfcUserNameLabel)
        contentView.addSubview(qfcLastMessageLabel)
        contentView.addSubview(qfcTimestampLabel)
        contentView.addSubview(qfcUnreadBadge)
        qfcUnreadBadge.addSubview(qfcUnreadCountLabel)
        
        // 布局
        qfcAvatarImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(50)
        }
        
        qfcUserNameLabel.snp.makeConstraints { make in
            make.top.equalTo(qfcAvatarImageView).offset(2)
            make.left.equalTo(qfcAvatarImageView.snp.right).offset(15)
            make.right.equalTo(qfcTimestampLabel.snp.left).offset(-10)
        }
        
        qfcLastMessageLabel.snp.makeConstraints { make in
            make.top.equalTo(qfcUserNameLabel.snp.bottom).offset(5)
            make.left.equalTo(qfcUserNameLabel)
            make.right.equalTo(contentView).offset(-15)
        }
        
        qfcTimestampLabel.snp.makeConstraints { make in
            make.top.equalTo(qfcUserNameLabel)
            make.right.equalToSuperview().offset(-15)
            make.width.equalTo(80)
        }
        
        qfcUnreadBadge.snp.makeConstraints { make in
            make.top.equalTo(qfcAvatarImageView).offset(-5)
            make.right.equalTo(qfcAvatarImageView).offset(5)
            make.width.height.equalTo(20)
        }
        
        qfcUnreadCountLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    // MARK: - 配置
    func qfcConfigure(with conversation: FCCConversation) {
        // 设置头像
        if let systemImage = UIImage(systemName: conversation.qfcIsSystemMessage ? "bell" : (conversation.qfcIsInteractiveMessage ? "person.2" : "person")) {
            qfcAvatarImageView.image = systemImage
            qfcAvatarImageView.tintColor = .darkGray
        }
        
        // 设置用户名
        qfcUserNameLabel.text = conversation.qfcUserName
        
        // 设置最后一条消息
        qfcLastMessageLabel.text = conversation.qfcGetLastMessageText()
        
        // 设置时间戳
        qfcTimestampLabel.text = qfcFormatTimestamp(conversation.qfcTimestamp)
        
        // 设置未读消息数
        if conversation.qfcUnreadCount > 0 {
            qfcUnreadBadge.isHidden = false
            qfcUnreadCountLabel.text = conversation.qfcUnreadCount > 99 ? "99+" : "\(conversation.qfcUnreadCount)"
        } else {
            qfcUnreadBadge.isHidden = true
        }
    }
    
    // MARK: - 辅助方法
    private func qfcFormatTimestamp(_ date: Date) -> String {
        let calendar = Calendar.current
        if calendar.isDateInToday(date) {
            let formatter = DateFormatter()
            formatter.dateFormat = "HH:mm"
            return formatter.string(from: date)
        } else if calendar.isDateInYesterday(date) {
            return "Yesterday"
        } else {
            let formatter = DateFormatter()
            formatter.dateFormat = "MM/dd"
            return formatter.string(from: date)
        }
    }
    
    // 取消正在进行的图片加载操作
    func qfcCancelImageLoading() {
        // 如果有异步加载图片的操作，可以在这里取消
    }
}